﻿using Newtonsoft.Json;
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Net.WebSockets;
using System.Text;
using System.Threading;

namespace DuBaiCoffee2.Model
{
    public class SocketClients
    {
        /// <summary>
        /// 左边
        /// </summary>
        public int ZBBS = 0;
        /// <summary>
        /// 右边
        /// </summary>
        public int YBBS = 0;
        private static SocketClients instance;
        private SocketClients() { }
        public static SocketClients Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new SocketClients();
                }
                return instance;
            }
        }
        /// <summary>
        /// 创建客户端
        /// </summary>
        public Socket client;

        public void socketclient()
        {
            try
            {
                ///创建客户端
                client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
                ///IP地址
                IPAddress ip = IPAddress.Parse(IPorPortInfo.Instance.CoffeeIP);
                ///端口号
                IPEndPoint endPoint = new IPEndPoint(ip, 8885);
                ///建立与服务器的远程连接
                client.Connect(endPoint);
                ClientList clientList = new ClientList();
                clientList.MsgBol = WuLiaoCoffeeSel.Instance;
                clientList.code = 4;
                client.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(clientList)));
                ///线程问题
                Thread thread = new Thread(ReciveMsg);
                thread.IsBackground = true;
                thread.Start(client);
            }
            catch (Exception)
            {
                return;
            }
        }
        /// <summary>
        /// 客户端接收到服务器发送的消息
        /// </summary>
        /// <param name="o">客户端</param>
        void ReciveMsg(object o)
        {
            Socket client = o as Socket;
            while (true)
            {
                try
                {
                    ///定义客户端接收到的信息大小
                    byte[] arrList = new byte[1024 * 1024];
                    ///接收到的信息大小(所占字节数)
                    int length = client.Receive(arrList);
                    string msg = Encoding.UTF8.GetString(arrList, 0, length);
                    var ClientList = JsonConvert.DeserializeObject<ClientList>(msg);
                    if (ClientList != null)
                    {
                        if (ClientList.code == 0)
                        {
                            show(ClientList.message.type, ClientList.message.Name);
                        }
                        else if (ClientList.code == 3)
                        {
                            RobotShow(ClientList.message.Name, ClientList.message.type);
                            ClientList clientList = new ClientList();
                            clientList.MsgBol = WuLiaoCoffeeSel.Instance;
                            clientList.robotMsg = RobotMsg.Instance;
                            clientList.code = 3;
                            clientList.IsMake = MainWindow.isMake;
                            var list = JsonConvert.SerializeObject(clientList);
                            var msgs = Encoding.UTF8.GetBytes(list);
                            client.Send(msgs);
                        }
                        else if (ClientList.code == 6)
                        {
                            MainWindow.MakingTypes = ClientList.message.type;
                            MainWindow.img_path = ClientList.message.Name;
                        }
                    }
                }
                catch (Exception)
                {
                    ///关闭客户端
                    client.Close();
                }

            }
        }
        public void show(Double code, string msg)
        {
            try
            {
                IOMsg.Instance.AllBol = false;
                Thread.Sleep(100);
                switch (msg)
                {
                    case "TGS_BTN":
                        if (code == 0)
                        {
                            IOMsg.Instance.AllBol = false;
                            Thread.Sleep(100);
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_UP_end);
                            Thread.Sleep(100);
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_Down_end);
                            Thread.Sleep(100);

                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_UP_start);

                            Thread.Sleep(100);
                            IOMsg.Instance.AllBol = true;
                        }
                        else
                        {
                            IOMsg.Instance.AllBol = false;
                            Thread.Sleep(100);
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_UP_end);
                            Thread.Sleep(100);
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_Down_end);
                            Thread.Sleep(100);
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_UP_end);
                            Thread.Sleep(100);
                            IOMsg.Instance.AllBol = true;
                        }
                        break;
                    case "TGX_BTN":
                        if (code == 0)
                        {
                            IOMsg.Instance.AllBol = false;
                            Thread.Sleep(100);
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_UP_end);
                            Thread.Sleep(100);
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_Down_end);
                            Thread.Sleep(100);
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_Down_start);
                            Thread.Sleep(100);
                            IOMsg.Instance.AllBol = true;
                        }
                        else
                        {
                            IOMsg.Instance.AllBol = false;
                            Thread.Sleep(100);
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_UP_end);
                            Thread.Sleep(100);
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_Down_end);
                            Thread.Sleep(100);
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_Down_end);
                            Thread.Sleep(100);
                            IOMsg.Instance.AllBol = true;
                        }
                        break;
                    case "LB8":
                        if (code == 0)
                        {
                            IOMsg.Instance.AllBol = false;
                            Thread.Sleep(100);
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.Drop_Cup_start);
                            Thread.Sleep(100);
                            IOMsg.Instance.AllBol = true;
                        }
                        else
                        {
                            IOMsg.Instance.AllBol = false;
                            Thread.Sleep(100);
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.Drop_Cup_end);
                            Thread.Sleep(100);
                            IOMsg.Instance.AllBol = true;
                        }
                        break;
                    case "LB12":
                        if (code == 0)
                        {
                            IOMsg.Instance.AllBol = false;
                            Thread.Sleep(100);
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.Drop_Cup2_start);
                            Thread.Sleep(100);
                            IOMsg.Instance.AllBol = true;
                        }
                        else
                        {
                            IOMsg.Instance.AllBol = false;
                            Thread.Sleep(100);
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.Drop_Cup2_end);
                            Thread.Sleep(100);
                            IOMsg.Instance.AllBol = true;
                        }
                        break;
                    case "CCMSS":
                        if (code == 0)
                        {
                            IOMsg.Instance.AllBol = false;
                            Thread.Sleep(100);
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_UP_start);
                            Thread.Sleep(100);
                            IOMsg.Instance.AllBol = true;
                        }
                        else
                        {
                            IOMsg.Instance.AllBol = false;
                            Thread.Sleep(100);
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_UP_end);
                            Thread.Sleep(100);
                            IOMsg.Instance.AllBol = true;
                        }
                        break;
                    case "CCMXJ":
                        if (code == 0)
                        {
                            IOMsg.Instance.AllBol = false;
                            Thread.Sleep(100);
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_Down_start);
                            Thread.Sleep(100);
                            IOMsg.Instance.AllBol = true;
                        }
                        else
                        {
                            IOMsg.Instance.AllBol = false;
                            Thread.Sleep(100);
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_Down_end);
                            Thread.Sleep(100);
                            IOMsg.Instance.AllBol = true;
                        }
                        break;
                    case "GTZ1":
                        if (code == 0)
                        {
                            IOMsg.Instance.AllBol = false;
                            Thread.Sleep(100);
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.GT1_start);
                            Thread.Sleep(100);
                            IOMsg.Instance.AllBol = true;
                        }
                        else
                        {
                            IOMsg.Instance.AllBol = false;
                            Thread.Sleep(100);
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.GT1_end);
                            Thread.Sleep(100);
                            IOMsg.Instance.AllBol = true;
                        }
                        break;
                    case "GTZ2":
                        if (code == 0)
                        {
                            IOMsg.Instance.AllBol = false;
                            Thread.Sleep(100);
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.GT2_start);
                            Thread.Sleep(100);
                            IOMsg.Instance.AllBol = true;
                        }
                        else
                        {
                            IOMsg.Instance.AllBol = false;
                            Thread.Sleep(100);
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.GT2_end);
                            Thread.Sleep(100);
                            IOMsg.Instance.AllBol = true;
                        }
                        break;
                    case "GTZ3":
                        if (code == 0)
                        {
                            IOMsg.Instance.AllBol = false;
                            Thread.Sleep(100);
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.GT3_start);
                            Thread.Sleep(100);
                            IOMsg.Instance.AllBol = true;
                        }
                        else
                        {
                            IOMsg.Instance.AllBol = false;
                            Thread.Sleep(100);
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.GT3_end);
                            Thread.Sleep(100);
                            IOMsg.Instance.AllBol = true;
                        }
                        break;
                    case "GT1":
                        MainWindow.SugarInit1 = code == 0 ? 1 : code;
                        WuLiaoCoffeeSel.Instance.SugarInit1 = code == 0 ? 1 : code;
                        IPorPortInfo.Instance.SugarInit1 = code == 0 ? 1 : code;
                        IPorPortInfo porPortInfo1 = IPorPortInfo.Instance;
                        File.WriteAllText(MainWindow.IPorPortUrl, JsonConvert.SerializeObject(porPortInfo1));

                        break;
                    case "CSGT1":
                        IOMsg.Instance.AllBol = false;
                        Thread.Sleep(100);
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.GT1_start);
                        Thread.Sleep(Convert.ToInt32(code == 0 ? 1 : code) * 1000);
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.GT1_end);
                        Thread.Sleep(100);
                        IOMsg.Instance.AllBol = true;
                        break;
                    case "GT2":
                        MainWindow.SugarInit2 = code == 0 ? 1 : code;
                        WuLiaoCoffeeSel.Instance.SugarInit2 = code == 0 ? 1 : code;
                        IPorPortInfo.Instance.SugarInit2 = code == 0 ? 1 : code;
                        IPorPortInfo porPortInfo2 = IPorPortInfo.Instance;
                        File.WriteAllText(MainWindow.IPorPortUrl, JsonConvert.SerializeObject(porPortInfo2));
                        break;
                    case "CSGT2":
                        IOMsg.Instance.AllBol = false;
                        Thread.Sleep(100);
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.GT2_start);
                        Thread.Sleep(Convert.ToInt32(code == 0 ? 1 : code) * 1000);
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.GT2_end);
                        Thread.Sleep(100);
                        IOMsg.Instance.AllBol = true;
                        break;
                    case "GT3":
                        MainWindow.SugarInit3 = code == 0 ? 1 : code;
                        WuLiaoCoffeeSel.Instance.SugarInit3 = code == 0 ? 1 : code;
                        IPorPortInfo.Instance.SugarInit3 = code == 0 ? 1 : code;
                        IPorPortInfo porPortInfo3 = IPorPortInfo.Instance;
                        File.WriteAllText(MainWindow.IPorPortUrl, JsonConvert.SerializeObject(porPortInfo3));

                        break;
                    case "CSGT3":
                        IOMsg.Instance.AllBol = false;
                        Thread.Sleep(100);
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.GT3_start);
                        Thread.Sleep(Convert.ToInt32(code == 0 ? 1 : code) * 1000);
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.GT3_end);
                        Thread.Sleep(100);
                        IOMsg.Instance.AllBol = true;
                        break;
                    default:
                        break;
                }
                Thread.Sleep(100);
                IOMsg.Instance.AllBol = true;
                ClientList clientList = new ClientList();
                MessageClientList coffeelist = MessageClientList.Instance;
                coffeelist.Name = msg;
                coffeelist.type = code == 1 ? 0 : 1;
                clientList.message = coffeelist;
                clientList.code = 0;
                client.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(clientList)));
            }
            catch (Exception)
            {
            }
        }
        /// <summary>
        ///   状态  0 关状态  1 开状态 2 已开机 3 上电 4 下电 5 已使能 6 下使能 7 已运行 8 继续运行 9 已报警 10 已暂停  11已停止 ,12 机械臂状态查询
        /// </summary>
        /// <param name="name"></param>
        /// <param name="Type"></param>
        public void RobotShow(string name, double Type)
        {
            try
            {
                switch (Type)
                {
                    case 3:
                        jakaAPI.power_on(ref RobotOperate.handle1);
                        break;
                    case 4:
                        jakaAPI.power_off(ref RobotOperate.handle1);
                        break;
                    case 5:
                        jakaAPI.enable_robot(ref RobotOperate.handle1);
                        break;
                    case 6:
                        jakaAPI.disable_robot(ref RobotOperate.handle1);
                        break;
                    case 7:
                        jakaAPI.program_run(ref RobotOperate.handle1);
                        break;
                    case 8:
                        jakaAPI.program_resume(ref RobotOperate.handle1);
                        break;
                    case 9:
                        jakaAPI.clear_error(ref RobotOperate.handle1);
                        break;
                    case 10:
                        jakaAPI.program_pause(ref RobotOperate.handle1);
                        break;
                    case 11:
                        jakaAPI.program_abort(ref RobotOperate.handle1);
                        break;
                }
                RobotOperate.Instance.Robot_L_Show();
            }
            catch (Exception)
            {
            }
        }
        /// <summary>
        /// 取杯
        /// </summary>
        /// <param name="cupCode"></param>
        public static void QBMsg()
        {
            try
            {
                IOMsg.Instance.AllBol = false;
                Thread.Sleep(100);
                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_DJBD_UP);
                Thread.Sleep(100);
                while (true)
                {
                    Console.WriteLine("daojiebeidian1 " + DateTime.Now.ToString());
                    IOMsg.Instance.RobotBol = false;
                    Thread.Sleep(100);
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_DDLBQ1);
                    Thread.Sleep(100);
                    if (IOMsg.Instance.RobotBol)
                    {
                        CoffeeMakeModel.Instance.IsDJBDL = true;
                        Thread.Sleep(100);
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_KFJ_Down);
                        Thread.Sleep(100);
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_DY_Down);
                        Thread.Sleep(100);
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_LHR_Down);
                        Thread.Sleep(100);
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_JBR_Down);
                        Thread.Sleep(100);
                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_WBTZ_Down);
                        Thread.Sleep(100);
                        break;
                    }
                }
                Thread.Sleep(200);
                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.Drop_Cup_start);
                Thread.Sleep(200);
                IOMsg.Instance.AllBol = true;
                while (WuLiaoCoffeeSel.Instance.LBQFK1_Bol) { Thread.Sleep(10); }
                while (!WuLiaoCoffeeSel.Instance.LBQFK1_Bol) { Thread.Sleep(10); }
                IOMsg.Instance.AllBol = false;
                Thread.Sleep(200);
                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.Drop_Cup_end);
                Thread.Sleep(200);
                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_DJCD_UP);
                Thread.Sleep(200);
                IOMsg.Instance.RobotBol = false;
                while (true)
                {
                    Thread.Sleep(200);
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_DJBJC);
                    Thread.Sleep(200);
                    if (IOMsg.Instance.RobotBol)
                    {
                        break;
                    }
                }
                IOMsg.Instance.AllBol = true;
            }
            catch (Exception)
            {
                return;
            }
        }
        /// <summary>
        /// 获取网络图片转换为base64
        /// </summary>
        /// <param name="url">网络图片路径   例：http://123.123.123.123/a.jpg</param>
        /// <returns></returns>
        public string GetUrlToBase64(string url)
        {
            try
            {
                var base64String = "";
                HttpWebRequest webRequest = WebRequest.Create(url) as HttpWebRequest;
                HttpWebResponse webResponse = webRequest.GetResponse() as HttpWebResponse;
                string base64sstr = string.Empty;
                using (Stream stream = webResponse.GetResponseStream())
                {
                    var mStream = new MemoryStream();

                    byte[] buffer = new byte[1024];
                    int byteCount;
                    do
                    {
                        byteCount = stream.Read(buffer, 0, buffer.Length);
                        mStream.Write(buffer, 0, byteCount);
                    } while (byteCount > 0);

                    mStream.Seek(0, SeekOrigin.Begin);
                    base64String = Convert.ToBase64String(mStream.ToArray());
                }
                //return base64String;
                WebClient mywebclient = new WebClient();
                byte[] Bytes = mywebclient.DownloadData(url);
                string Base64string = Convert.ToBase64String(Bytes);
                return "data:image/jpeg;base64," + base64String;
            }
            catch (Exception e)
            {
                return "";
            }
        }
        public void ClientR()
        {
            try
            {
                while (true)
                {
                    try
                    {
                        if (MainWindow.IsStartMakingR)
                        {
                            MainWindow.IsStartMakingR = false;
                            IOMsgR.Instance.RobotBol = false;
                            while (true)
                            {
                                Thread.Sleep(100);
                                IOSerialPortR.Instance.SerialSend(DigitalMsg.Instance.RRobot_DYD);
                                Thread.Sleep(100);
                                if (IOMsgR.Instance.RobotBol)
                                {
                                    IOMsgR.Instance.QK();
                                    break;
                                }
                            }
                            int code = int.TryParse(MainWindow.coffeeNameCode, out int a) ? a : 5001;
                            int codeMid = code / 100;
                            Thread.Sleep(200);
                            if (codeMid == 62)//加冰品
                            {
                                IOSerialPortR.Instance.SerialSend(DigitalMsg.Instance.LRobot_JBR_UP);
                                while (true)
                                {
                                    Thread.Sleep(100);
                                    IOSerialPortR.Instance.SerialSend(DigitalMsg.Instance.RRobot_DZBJ);
                                    Thread.Sleep(100);
                                    if (IOMsgR.Instance.RobotBol)
                                    {
                                        break;
                                    }
                                }
                                IceMakingMsg.Instance.AllBol = false;
                                Thread.Sleep(100);
                                IceMakingSerialPort.Instance.SerialSend(IceMakingModel.Instance.IceMaking);
                                Thread.Sleep(100);
                                IceMakingMsg.Instance.AllBol = true;
                                Thread.Sleep(5000);
                                IOSerialPortR.Instance.SerialSend(DigitalMsg.Instance.LRobot_CBWCR_UP);
                                Thread.Sleep(100);
                                while (true)
                                {
                                    Thread.Sleep(100);
                                    IOSerialPortR.Instance.SerialSend(DigitalMsg.Instance.RRobot_DDBD);
                                    Thread.Sleep(100);
                                    if (IOMsgR.Instance.RobotBol)
                                    {
                                        break;
                                    }
                                }
                                while (!CoffeeMakeModel.Instance.IsDJBL) { Thread.Sleep(10); }
                                Thread.Sleep(100);
                                IOSerialPortR.Instance.SerialSend(DigitalMsg.Instance.LRobot_DBR_UP);
                                Thread.Sleep(100);
                                while (true)
                                {
                                    Thread.Sleep(100);
                                    IOSerialPortR.Instance.SerialSend(DigitalMsg.Instance.RRobot_DBWC);
                                    Thread.Sleep(100);
                                    if (IOMsgR.Instance.RobotBol)
                                    {
                                        break;
                                    }
                                }
                                CoffeeMakeModel.Instance.IsDBWCL = true;
                                YBBS = 1;
                            }
                            else if (codeMid == 63)//拉花品
                            {
                                IOSerialPortR.Instance.SerialSend(DigitalMsg.Instance.LRobot_LHR_UP);
                                Thread.Sleep(100);
                                //IOSerialPortR.Instance.SerialSend(DigitalMsg.Instance.LRobot_KFJR_UP);
                                while (true)
                                {
                                    Thread.Sleep(100);
                                    IOSerialPortR.Instance.SerialSend(DigitalMsg.Instance.RRobot_DKFJ);
                                    Thread.Sleep(100);
                                    if (IOMsgR.Instance.RobotBol)
                                    {
                                        break;
                                    }
                                }
                                while (!CoffeeMakeModel.Instance.IsDKFJL) { Thread.Sleep(100); }
                                CoffeeErversysMsg.Instance.iscoffeeready = false;
                                Thread.Sleep(200);
                                CoffeeErversysMsg.Instance.apiFunctions.DoProductOfDisplay(EversysApi.Defines.ProcSide_t.Left_e, MainWindow.coffeeNameCode == "6301" ? 9 : MainWindow.coffeeNameCode == "6302" ? 10 : 0);
                                Thread.Sleep(200);
                                CoffeeErversysMsg.Instance.iscoffeeready = true;
                                Thread.Sleep(5000);
                                while (!CoffeeErversysMsg.Instance.IsReadyMilkL) { Thread.Sleep(10); }
                                Thread.Sleep(2000);
                                IOSerialPortR.Instance.SerialSend(DigitalMsg.Instance.LRobot_JNWCR_UP);
                                Thread.Sleep(100);
                                while (true)
                                {
                                    Thread.Sleep(100);
                                    IOSerialPortR.Instance.SerialSend(DigitalMsg.Instance.RRobot_DLHD);
                                    Thread.Sleep(100);
                                    if (IOMsgR.Instance.RobotBol)
                                    {
                                        break;
                                    }
                                }
                                CoffeeMakeModel.Instance.IsDLHDR = true;
                                YBBS = 1;
                                while (!CoffeeMakeModel.Instance.IsLHWCR) { Thread.Sleep(10); }
                                while (RobotOperate.pstatusR == JKTYPE.ProgramState.PROGRAM_RUNNING) { Thread.Sleep(10); }
                                Thread.Sleep(100);
                                IOSerialPortR.Instance.SerialSend(DigitalMsg.Instance.LRobot_XBR_UP);
                                Thread.Sleep(100);
                                while (true)
                                {
                                    Thread.Sleep(100);
                                    IOSerialPortR.Instance.SerialSend(DigitalMsg.Instance.RRobot_QXBD);
                                    Thread.Sleep(100);
                                    if (IOMsgR.Instance.RobotBol)
                                    {
                                        break;
                                    }
                                }
                                Thread.Sleep(100);
                                IOSerialPortR.Instance.SerialSend(DigitalMsg.Instance.LRobot_XBR_Down);
                                Thread.Sleep(100);
                                CoffeeMakeModel.Instance.IsDXBDR = true;
                            }
                            
                        }
                    }
                    catch (Exception)
                    {
                    }

                    Thread.Sleep(1000);
                }

            }
            catch (Exception)
            {
            }
        }
        public void ClientL()
        {
            try
            {
                while (true)
                {
                    try
                    {
                        if (MainWindow.IsStartMakingL)
                        {
                            MainWindow.IsStartMakingL = false;
                            IOMsg.Instance.AllBol = false;
                            IOMsg.Instance.RobotBol = false;
                            Thread.Sleep(100);
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_DYD);
                            Thread.Sleep(100);
                            if (IOMsg.Instance.RobotBol)
                            {
                                IOMsg.Instance.QK();
                            }
                            int code = int.TryParse(MainWindow.coffeeNameCode, out int a) ? a : 6001;
                            int codeMid = code / 100;
                            Thread.Sleep(200);
                            if (codeMid == 60)//直接饮品
                            {
                                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_KFJ_UP);
                            }
                            else if (codeMid == 61)//打印品
                            {
                                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_DY_UP);
                            }
                            else if (codeMid == 62)//加冰品
                            {
                                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_JB_UP);
                            }
                            else if (codeMid == 63)//拉花品
                            {
                                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_LH_UP);
                            }
                            Thread.Sleep(100);
                            QBMsg();
                            Thread.Sleep(200);
                            IOMsg.Instance.AllBol = true;
                            Thread.Sleep(200);
                            while (true)
                            {
                                if (WuLiaoCoffeeSel.Instance.TakeCup_Sel_Bol)
                                {
                                    IOMsg.Instance.AllBol = false;
                                    Thread.Sleep(200);
                                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_JBCG_UP);
                                    Thread.Sleep(200);
                                    //50 接咖啡 51 打印 52 加冰 53 拉花
                                    if (codeMid == 60)
                                    {
                                        IOMsg.Instance.AllBol = false;
                                        IOMsg.Instance.RobotBol = false;
                                        while (true)
                                        {
                                            Thread.Sleep(100);
                                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_DYD);
                                            Thread.Sleep(100);
                                            if (IOMsg.Instance.RobotBol)
                                            {
                                                break;
                                            }
                                        }
                                        Thread.Sleep(100);
                                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_QDYJ_Down);
                                        Thread.Sleep(100);
                                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_DY_Down);
                                        Thread.Sleep(100);
                                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_QJKF_UP);
                                        Thread.Sleep(100);
                                        CoffeeErversysMsg.Instance.iscoffeeready = false;
                                        Thread.Sleep(200);
                                        CoffeeErversysMsg.Instance.apiFunctions.DoProductOfDisplay(EversysApi.Defines.ProcSide_t.Left_e, MainWindow.coffeeNameCode == "6001" ? 5 : MainWindow.coffeeNameCode == "6002" ? 6 : MainWindow.coffeeNameCode == "6003" ? 7 : MainWindow.coffeeNameCode == "6004" ? 8 : 0);
                                        Thread.Sleep(200);
                                        CoffeeErversysMsg.Instance.iscoffeeready = true;
                                        Console.WriteLine("yes3");
                                        Thread.Sleep(100);
                                        IOMsg.Instance.AllBol = true;
                                        Thread.Sleep(5000);
                                        while (!CoffeeErversysMsg.Instance.IsReadyMilkL) { Thread.Sleep(10); }
                                        Thread.Sleep(2000);
                                        IOMsg.Instance.AllBol = false;
                                        Thread.Sleep(100);
                                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_KFJW_UP);
                                        Thread.Sleep(100);
                                        IOMsg.Instance.AllBol = true;
                                        Thread.Sleep(100);
                                        ZBBS = 1;
                                    }
                                    else if (codeMid == 61)
                                    {
                                        IOMsg.Instance.AllBol = false;
                                        IOMsg.Instance.RobotBol = false;
                                        PwebSockets.isPrinter = false;
                                        CoffeeErversysMsg.Instance.iscoffeeready = false;
                                        Thread.Sleep(200);
                                        CoffeeErversysMsg.Instance.apiFunctions.DoProductOfDisplay(EversysApi.Defines.ProcSide_t.Left_e, MainWindow.coffeeNameCode == "6103" ? 7 : MainWindow.coffeeNameCode == "6104" ? 8 : 0);
                                        Thread.Sleep(200);
                                        CoffeeErversysMsg.Instance.iscoffeeready = true;
                                        //Thread.Sleep(3000);
                                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_QDYJ_UP);
                                        Thread.Sleep(100);
                                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_DY_UP);
                                        Thread.Sleep(100);
                                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_QJKF_UP);
                                        Thread.Sleep(100);
                                        IOMsg.Instance.AllBol = true;
                                        Thread.Sleep(5000);
                                        while (!CoffeeErversysMsg.Instance.IsReadyMilkL) { Thread.Sleep(10); }
                                        Thread.Sleep(5000);
                                        Console.WriteLine(CoffeeErversysMsg.Instance.IsReadyMilkL);
                                        IOMsg.Instance.AllBol = false;
                                        Thread.Sleep(100);
                                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_KFJW_UP);
                                        Thread.Sleep(100);
                                        Thread.Sleep(1000);
                                        IOMsg.Instance.RobotBol = false;
                                        Thread.Sleep(1000);
                                        while (true)
                                        {
                                            Thread.Sleep(100);
                                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_DDDYJ);
                                            Thread.Sleep(100);
                                            if (IOMsg.Instance.RobotBol)
                                            {
                                                break;
                                            }
                                        }
                                        IOMsg.Instance.AllBol = true;
                                        try
                                        {
                                            PwebSockets.pSocketClient.pbol = false;
                                            PwebSockets.IsReviecePrinter = false;
                                            if (PwebSockets.pSocketClient.State != WebSocketState.Open)
                                            {
                                                PwebSockets.pSocketClient.Close();
                                                PwebSockets.Pwebsocket();
                                                Thread.Sleep(2000);
                                            }
                                            PwebSockets.pSocketClient.Send(JsonConvert.SerializeObject(new PrinterSel { code = 1, tag = 1 }));
                                            Thread.Sleep(500);
                                            int sel = 0;
                                            bool ispbol = true;
                                            while (!PwebSockets.IsReviecePrinter)
                                            {
                                                sel++;
                                                Thread.Sleep(500);
                                                if (sel == 20)
                                                {
                                                    if (PwebSockets.pSocketClient.State != WebSocketState.Open)
                                                    {
                                                        PwebSockets.pSocketClient.Close();
                                                        PwebSockets.Pwebsocket();
                                                        Thread.Sleep(2000);
                                                        PwebSockets.pSocketClient.Send(JsonConvert.SerializeObject(new PrinterSel { code = 1, tag = 1 }));
                                                        Thread.Sleep(500);
                                                    }
                                                }
                                                if (sel == 40)
                                                {
                                                    ispbol = false;
                                                    IOMsg.Instance.AllBol = false;
                                                    Thread.Sleep(100);
                                                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_DYWC_UP);
                                                    Thread.Sleep(100);
                                                    IOMsg.Instance.AllBol = true;
                                                    PwebSockets.ErrorMsg = "";
                                                    break;
                                                }
                                            }
                                            if (ispbol)
                                            {
                                                if (PwebSockets.isPrinter)
                                                {
                                                    IOMsg.Instance.RobotBol = false;
                                                    PwebSockets.ErrorMsg = "";
                                                    if (PwebSockets.pSocketClient.State != WebSocketState.Open)
                                                    {
                                                        PwebSockets.pSocketClient.Close();
                                                        PwebSockets.Pwebsocket();
                                                        Thread.Sleep(2000);
                                                    }
                                                    PwebSockets.ErrorMsg = "dy";
                                                    int dyint = 0;
                                                    while (PwebSockets.ErrorMsg == "dy")
                                                    {
                                                        if (dyint < 10)
                                                        {
                                                            try
                                                            {
                                                                PrinterSel2 Printer_Set;
                                                                string imgname = GetUrlToBase64(MainWindow.Img);
                                                                Printer_Set = new PrinterSel2 { code = 2, tag = 1, data = new PrinterData { size = 70, img = imgname } };
                                                                PwebSockets.pSocketClient.Send(JsonConvert.SerializeObject(Printer_Set));
                                                            }
                                                            catch (Exception)
                                                            {
                                                            }
                                                            Thread.Sleep(2000);
                                                            dyint++;
                                                        }
                                                        else
                                                        {
                                                            PwebSockets.ErrorMsg = "over";
                                                            Thread.Sleep(2000);
                                                        }
                                                    }


                                                    PwebSockets.IsReviecePrinter = false;
                                                    Thread.Sleep(1000);
                                                    int imgindex = 0;
                                                    while (PwebSockets.ErrorMsg != "over" && PwebSockets.ErrorMsg != "ok1")
                                                    {
                                                        Thread.Sleep(1000);
                                                        imgindex++;
                                                        if (imgindex == 27)
                                                        {
                                                            if (PwebSockets.ErrorMsg == "")
                                                            {
                                                                PwebSockets.pSocketClient.Close();
                                                                PwebSockets.ErrorMsg = "over";
                                                                break;
                                                            }
                                                        }
                                                    }
                                                    if (PwebSockets.ErrorMsg == "over")
                                                    {
                                                        IOMsg.Instance.AllBol = false;
                                                        Thread.Sleep(500);
                                                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_DYWC_UP);
                                                        Thread.Sleep(500);
                                                        IOMsg.Instance.AllBol = true;
                                                        PwebSockets.ErrorMsg = "";
                                                    }
                                                    PwebSockets.pSocketClient.pbol = true;
                                                    IOMsg.Instance.RobotBol = true;
                                                }
                                            }
                                        }
                                        catch (Exception)
                                        {
                                            Thread.Sleep(60000);
                                            IOMsg.Instance.AllBol = false;
                                            Thread.Sleep(500);
                                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_DYWC_UP);
                                            Thread.Sleep(500);
                                            IOMsg.Instance.AllBol = true;
                                            PwebSockets.ErrorMsg = "";
                                        }
                                        ZBBS = 1;
                                    }
                                    else if (codeMid == 62)
                                    {
                                        IOMsg.Instance.AllBol = false;
                                        IOMsg.Instance.RobotBol = false;
                                        while (true)
                                        {
                                            Thread.Sleep(100);
                                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_DJBD);
                                            Thread.Sleep(100);
                                            if (IOMsg.Instance.RobotBol)
                                            {
                                                break;
                                            }
                                        }
                                        CoffeeMakeModel.Instance.IsDJBL = true;
                                        while (!CoffeeMakeModel.Instance.IsDBWCL) { Thread.Sleep(10); }
                                        Thread.Sleep(100);
                                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_QDYJ_Down);
                                        Thread.Sleep(100);
                                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_DY_Down);
                                        Thread.Sleep(100);
                                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_QJKF_UP);
                                        Thread.Sleep(100);
                                        CoffeeErversysMsg.Instance.iscoffeeready = false;
                                        Thread.Sleep(200);
                                        CoffeeErversysMsg.Instance.apiFunctions.DoProductOfDisplay(EversysApi.Defines.ProcSide_t.Left_e, MainWindow.coffeeNameCode == "6201" ? 5 : MainWindow.coffeeNameCode == "6202" ? 11 : 0);
                                        Thread.Sleep(200);
                                        CoffeeErversysMsg.Instance.iscoffeeready = true;
                                        Console.WriteLine("yes3");
                                        Thread.Sleep(100);
                                        IOMsg.Instance.AllBol = true;
                                        Thread.Sleep(5000);
                                        while (!CoffeeErversysMsg.Instance.IsReadyMilkL) { Thread.Sleep(10); }
                                        Thread.Sleep(2000);
                                        IOMsg.Instance.AllBol = false;
                                        Thread.Sleep(100);
                                        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_KFJW_UP);
                                        Thread.Sleep(100);
                                        IOMsg.Instance.AllBol = true;
                                        Thread.Sleep(100);
                                        ZBBS = 1;
                                    }
                                    else if (codeMid == 63)
                                    {
                                        try
                                        {
                                            IOMsg.Instance.AllBol = false;
                                            IOMsg.Instance.RobotBol = false;
                                            while (true)
                                            {
                                                Thread.Sleep(100);
                                                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_DYD);
                                                Thread.Sleep(100);
                                                if (IOMsg.Instance.RobotBol)
                                                {
                                                    break;
                                                }
                                            }
                                            Thread.Sleep(100);
                                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_QDYJ_Down);
                                            Thread.Sleep(100);
                                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_DY_Down);
                                            Thread.Sleep(100);
                                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_QJKF_UP);
                                            Thread.Sleep(100);
                                            while (true)
                                            {
                                                Thread.Sleep(100);
                                                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_DKFJ);
                                                Thread.Sleep(100);
                                                if (IOMsg.Instance.RobotBol)
                                                {
                                                    break;
                                                }
                                            }
                                            Thread.Sleep(100);
                                            CoffeeErversysMsg.Instance.iscoffeeready = false;
                                            Thread.Sleep(200);
                                            CoffeeErversysMsg.Instance.apiFunctions.DoProductOfDisplay(EversysApi.Defines.ProcSide_t.Left_e, 5);
                                            Thread.Sleep(200);
                                            CoffeeErversysMsg.Instance.iscoffeeready = true;
                                            Console.WriteLine("yes3");
                                            Thread.Sleep(100);
                                            IOMsg.Instance.AllBol = true;
                                            CoffeeMakeModel.Instance.IsDKFJL = true;
                                            Thread.Sleep(5000);
                                            while (!CoffeeErversysMsg.Instance.IsReadyMilkL) { Thread.Sleep(10); }
                                            Thread.Sleep(2000);
                                            IOMsg.Instance.AllBol = false;
                                            Thread.Sleep(100);
                                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_KFJW_UP);
                                            Thread.Sleep(100);
                                            while (true)
                                            {
                                                Thread.Sleep(100);
                                                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_DLHD);
                                                Thread.Sleep(100);
                                                if (IOMsg.Instance.RobotBol)
                                                {
                                                    break;
                                                }
                                            }
                                            Thread.Sleep(100);
                                            IOMsg.Instance.AllBol = true;
                                            Thread.Sleep(100);
                                            CoffeeMakeModel.Instance.IsDLHDL = true;
                                            ZBBS = 1;
                                            while (!CoffeeMakeModel.Instance.IsLHWCL) { Thread.Sleep(10); }
                                            while (CoffeeMakeModel.Instance.IsDLHDL) { Thread.Sleep(10); }
                                            while (RobotOperate.pstatusL == JKTYPE.ProgramState.PROGRAM_RUNNING) { Thread.Sleep(10); }
                                            IOMsg.Instance.AllBol = false;
                                            Thread.Sleep(100);
                                            IOSerialPortR.Instance.SerialSend(DigitalMsg.Instance.LRobot_LHCC_UP);
                                            Thread.Sleep(100);
                                            while (true)
                                            {
                                                Thread.Sleep(100);
                                                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_CCWC);
                                                Thread.Sleep(100);
                                                if (IOMsg.Instance.RobotBol)
                                                {
                                                    break;
                                                }
                                            }
                                            Thread.Sleep(100);
                                            IOSerialPortR.Instance.SerialSend(DigitalMsg.Instance.LRobot_LHCC_Down);
                                            Thread.Sleep(100);
                                            IOMsg.Instance.AllBol = true;
                                            CoffeeMakeModel.Instance.IsDCCL = true;
                                        }
                                        catch (Exception)
                                        {
                                        }
                                    }
                                    break;
                                }
                                else
                                {
                                    IOMsg.Instance.AllBol = false;
                                    Thread.Sleep(200);
                                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_WBTZ_UP);
                                    Thread.Sleep(200);
                                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_DJBD_Down);
                                    Thread.Sleep(200);
                                    IOMsg.Instance.AllBol = true;
                                    Thread.Sleep(200);
                                    QBMsg();
                                    IOMsg.Instance.AllBol = true;
                                    Thread.Sleep(200);
                                }
                                Thread.Sleep(10);
                            }
                        }
                    }
                    catch (Exception)
                    {
                    }
                    Thread.Sleep(1000);
                }
            }
            catch (Exception)
            {
            }
        }

        public void IsClear()
        {
            try
            {
                CoffeeMakeModel.Instance.IsDDBDR = false;
                CoffeeMakeModel.Instance.IsDKFJL = false;
                CoffeeMakeModel.Instance.IsLHWCR = false;
                CoffeeMakeModel.Instance.IsLHWCL = false;
                CoffeeMakeModel.Instance.IsDCCL = false;
                CoffeeMakeModel.Instance.IsDBWCL = false;
                CoffeeMakeModel.Instance.IsDJBDL = false;
                CoffeeMakeModel.Instance.IsDJBJCL = false;
                CoffeeMakeModel.Instance.IsDJBL = false;
                CoffeeMakeModel.Instance.IsDLHDL = false;
                CoffeeMakeModel.Instance.IsDLHDR = false;
            }
            catch (Exception)
            {
            }
        }
    }
}
